﻿using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Framework.UGUI
{
    public static class GuiHelper
    {
        /// <summary>
        /// 根据文本生成器的属性，通过传入字符串计算字符串的宽度
        /// </summary>
        /// <param name="txtCom">Text组件</param>
        /// <param name="str">尝试填入的字符串</param>
        /// <returns></returns>
        public static float GetPreferredWidth(Text txtCom, string str)
        {
            if (txtCom == null) return 0;
            TextGenerator txtGenerator = txtCom.cachedTextGeneratorForLayout;
            TextGenerationSettings textGenerationSettings = txtCom.GetGenerationSettings(txtCom.rectTransform.sizeDelta);
            return txtGenerator.GetPreferredWidth(str, textGenerationSettings);
        }
        /// <summary>
        /// 根据文本生成器的属性，通过传入字符串计算字符串的高度
        /// </summary>
        /// <param name="txtCom">Text组件</param>
        /// <param name="str">尝试填入的字符串</param>
        /// <returns></returns>
        public static float GetPreferredHeight(Text txtCom, string str)
        {
            if (txtCom == null) return 0;
            TextGenerator txtGenerator = txtCom.cachedTextGeneratorForLayout;
            TextGenerationSettings textGenerationSettings = txtCom.GetGenerationSettings(txtCom.rectTransform.sizeDelta);
            return txtGenerator.GetPreferredHeight(str, textGenerationSettings);
        }

        public static float GetPreferredWidth(TextMeshProUGUI txtCom, string str)
        {
            if (txtCom == null) return 0;
            txtCom.text = str;
            var height = txtCom.preferredWidth;
            txtCom.text = "";
            return height;
        }

        public static float GetPreferredHeight(TextMeshProUGUI txtCom, string str)
        {
            if (txtCom == null) return 0;
            txtCom.text = str;
            var height = txtCom.preferredHeight;
            txtCom.text = "";
            return height;
        }

        /// <summary>
        /// 设置Image、Text等颜色
        /// </summary>
        /// <param name="graphic">Image or Text</param>
        /// <param name="colorStr">如#FFA500</param>
        public static void SetColor(Graphic graphic, string colorStr)
        {
            Color color;
            if(ColorUtility.TryParseHtmlString(colorStr, out color))
            {
                graphic.color = color;
            }
        }

        /// <summary>
        /// 根据颜色字符串获取颜色结构体
        /// </summary>
        /// <param name="colorStr">如#FFA500</param>
        /// <returns></returns>
        public static Color ParseColor(string colorStr)
        {
            Color color;
            ColorUtility.TryParseHtmlString(colorStr, out color);
            return color;
        }

        /// <summary>
        /// 将颜色转换成16进制字符串（带#号）
        /// </summary>
        /// <param name="color"></param>
        /// <returns>如#FFA500</returns>
        public static string ColorToHex(Color color)
        {
            return "#" + ColorUtility.ToHtmlStringRGBA(color);
        }
    }
}

